package ngamejava2;

import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class GameWriter
{
	private BufferedWriter bw;

	public GameWriter(String fileName) throws IOException
	{
		bw = new BufferedWriter(new FileWriter(fileName));
		bw.write("﻿<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	}

	public void close() throws IOException
	{
		bw.close();
	}

	public void write(String name, BaseGame g) throws IOException
	{
		bw.write("<game name=\""+name+"\" class=\""+g.getClass().getName()+"\">");
		g.write(this);
		bw.write("</game>");
	}

	public void write(String name, GameObject obj) throws IOException
	{
		bw.write("<object name=\""+name+"\" class=\""+obj.getClass().getName()+"\">");
		obj.write(this);
		bw.write("</object>");
	}

	public void write(String name, Rectangle2D value) throws IOException
	{
		bw.write("<rectangle name=\""+name+"\" x=\""+value.getX()+"\" y=\""+value.getY()+"\" width=\""+value.getWidth()+"\" height=\""+value.getHeight()+"\" />");
	}

	public void write(String name, Point2D value) throws IOException
	{
		bw.write("<point name=\""+name+"\" x=\""+value.getX()+"\" y=\""+value.getY()+"\"/>");
	}

	public void write(String name, Polygon value) throws IOException
	{
		bw.write("<polygon name=\""+name+"\">");
		for (int i=0; i<value.npoints; i++)
		{
			this.write("p"+i, new Point2D.Float(value.xpoints[i], value.ypoints[i]));
		}
		bw.write("</polygon>");
	}

	public void write(String name, String value) throws IOException
	{
		bw.write("<string name=\""+name+"\">[!CDATA["+value+"]]</string>");
	}

	public void write(String name, boolean value) throws IOException
	{
		bw.write("<boolean name=\""+name+"\" value=\""+value+"\" />");
	}

	public void write(String name, float value) throws IOException
	{
		bw.write("<float name=\""+name+"\" value=\""+value+"\" />");
	}
}